import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class TableShowJPanel extends JScrollPane
{
    private final DefaultTableModel tableModel;

    public TableShowJPanel()
    {
        super();
        this.tableModel=new DefaultTableModel()
        {
            public boolean isCellEditable(int row,int column){return false;}
        };
        JTable jTable=new JTable(this.tableModel);
        jTable.setFillsViewportHeight(true);
        this.setViewportView(jTable);

    }

    public void setTable(ResultSet resultSet) throws SQLException
    {
        tableModel.setRowCount(0);
        tableModel.setColumnCount(0);
        ResultSetMetaData rm=resultSet.getMetaData();
        int count=rm.getColumnCount();
        for(int i=1;i<=count;++i)
            this.tableModel.addColumn(rm.getColumnLabel(i));
        while(resultSet.next())
        {
            Object[] result=new Object[count];
            for(int i=1;i<=count;++i)
                result[i-1]=resultSet.getString(i);
            this.tableModel.addRow(result);
        }
    }
}
